package com.inifitness.common.result;

import java.io.Serializable;

import lombok.Data;

/**
 * 统一封装请求结果
 *
 * @author sunjinfeng
 * @version 1.0.0
 * @since 2024/04/18 11:00:00
 */
@Data
public class Result<T> implements Serializable {

  private String code;
  private T data;
  private String msg;

  public static <T> Result<T> success() {
    return success(null);
  }

  public static <T> Result<T> success(T data) {
    Result<T> result = new Result<>();
    result.setCode(ResultCode.SUCCESS.getCode());
    result.setMsg(ResultCode.SUCCESS.getMsg());
    result.setData(data);
    return result;
  }

  public static <T> Result<T> failed() {
    return result(ResultCode.SYSTEM_EXECUTION_ERROR.getCode(),
        ResultCode.SYSTEM_EXECUTION_ERROR.getMsg(), null);
  }

  public static <T> Result<T> failed(String msg) {
    return result(ResultCode.SYSTEM_EXECUTION_ERROR.getCode(), msg, null);
  }

  public static <T> Result<T> judge(boolean status) {
    if (status) {
      return success();
    } else {
      return failed();
    }
  }

  public static <T> Result<T> failed(IResultCode resultCode) {
    return result(resultCode.getCode(), resultCode.getMsg(), null);
  }

  public static <T> Result<T> failed(IResultCode resultCode, String msg) {
    return result(resultCode.getCode(), msg, null);
  }

  @SuppressWarnings("unused")
  private static <T> Result<T> result(IResultCode resultCode, T data) {
    return result(resultCode.getCode(), resultCode.getMsg(), data);
  }

  private static <T> Result<T> result(String code, String msg, T data) {
    Result<T> result = new Result<>();
    result.setCode(code);
    result.setData(data);
    result.setMsg(msg);
    return result;
  }

  @SuppressWarnings("unused")
  public static boolean isSuccess(Result<?> result) {
    return result != null && ResultCode.SUCCESS.getCode().equals(result.getCode());
  }
}
